<?php
namespace jy_supplier;
defined('BY_JYA') or exit('error');
/*
	订单，V8打印机：58mm（32字符），V9：80mm（42字符）
	设置字体大小、居中及切纸、来单提示音：http://www.jinyunzn.com/manage/index.php?plugin=core&action=article.show&nosession=1&id=11（出处：进云手册）
*/
class printmsg extends \plugin{
	public function goodsprint($goods=array(),$log=array()){
		$isprint=false;
		$depot = pdo_get('jy_supplier_depot',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['depot_id']));
		$printer=pdo_get('jy_weishop_printer',array('print_type'=>4,'merchid'=>$log['merchid'],'pluginname'=>'jy_supplier','uniacid'=>$_SESSION['uniacid']));
		if(!$printer['id']){
			exi('没有合适的打印任务！','error');
		}
		$feieyun_print=pdo_get('jy_feieyun_print',array('id'=>$printer['printerid']));
		if(!$feieyun_print['id']){
			exi('打印任务没有绑定的打印机！','error');
		}
		$merch = pdo_get('jy_supplier_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['merchid']));
		$line = pdo_get('jy_supplier_line',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['line_id']));
		$gn=$depot['sn'].'-'.$line['sn'];
		$gn=$gn?:$this->setting['print_gn'];
		$gn=$gn?:'G-001';
		$carsn=$line['carsn']?:'C-001';
		for($i=1;$i<=$goods['printnum'];$i++){
			$key=TIMESTAMP+$i;
			//$goodsurl=custom_url('jy_supplier/goodslog/up/id='.$log['id'].'&goodsid='.$goods['goodsid'].'&optionid='.$goods['optionid'].'&key='.$key,true);
			$goodsurl='id='.$log['id'].'&goodsid='.$goods['goodsid'].'&optionid='.$goods['optionid'].'&key='.$key;
			//标签高：200；宽：380
			if($feieyun_print['type']){
				$goods['title']=cfc('tools')->substr($goods['title'],0,10);
				$cname=$merch['merchname']?:'供货商的名称';
				$datime=date('Y-m-d');
				$mobile=$this->setting['print_mobile']?:'400-XXX-888';
				//$sn=model('jy_feieyun/model')->LR('ID：'.$goods['goodsid'],$i.'/'.$goods['printnum'],26);
				$sn='SN'.sprintf("%010d", $log['id']);
				$content = '<TEXT x="9" y="10" font="12" w="2" h="2" r="0">'.$gn.'</TEXT>';
				$content .= '<TEXT x="9" y="65" font="12" w="0.8" h="0.8" r="0">'.$sn.'</TEXT>';
				$content .= '<TEXT x="9" y="85" font="12" w="0.8" h="0.8" r="0">'.$goods['title'].'</TEXT>';
				if($goods['leititle']){
					$goods['leititle']=cfc('tools')->substr($goods['leititle'],0,8);
					$content .= '<TEXT x="9" y="110" font="12" w="0.8" h="0.8" r="0">['.$goods['leititle'].']</TEXT>';
				}
				//右侧二维码
				$content .='<QR x="250" y="10" e="L" w="2">'.$goodsurl.'</QR>';
				$content .='<TEXT x="230" y="80" font="12" w="2" h="2" r="0">'.$carsn.'</TEXT>';
				//底部左侧
				$content .='<QR x="10" y="140"  e="L" w="3">'.$goodsurl.'</QR>';
				$content .= '<TEXT x="100" y="140" font="12" w="0.6" h="0.6" r="0">'.$cname.'</TEXT>';
				$content .= '<TEXT x="100" y="170" font="12" w="0.6" h="0.6" r="0">时间:'.$datime.'</TEXT>';
				$content .= '<TEXT x="100" y="200" font="12" w="0.6" h="0.6" r="0">电话:'.$mobile.'</TEXT>';
			}else{
				$goodsurl=custom_url('jy_supplier/goodslog/up/id='.$log['id'].'&goodsid='.$goods['goodsid'].'&optionid='.$goods['optionid'].'&key='.$key,true);
				$content = $depot['title']."<BR>";
				$content .="<B>".$goods['title']."</B><BR>";
				$content .='--------------------------------<BR>';
				$content .='<QR>'.$goodsurl.'</QR>';
				$content .= $cname."<BR>";
				$content .= '打印时间:'.$datime."<BR>";
				$content .= '客服电话:'.$mobile."<BR>";
			}
			//print_r($content);exit;
			$this->toprint($printer,$content,$log['merchid']);
			$isprint=true;
		}
		return $isprint;
	}
	public function toprint($p,$content='',$merchid=''){
		$setting=$this->plugin_setting('setting'.$merchid);
		model('jy_feieyun/model')->toprint($p['printerid'],$content,true,1,$setting,true);
		return true;
	}
	public function strlen_pad($str,$alllen)//补齐空格
	{
		//真实字符串长度（一个中文=2）
		$len = (strlen($str)+mb_strlen($str,'UTF-8'))/2;
		$buqi = '';
		for($i=0;$i<($alllen-$len);$i++){
			$buqi .= ' ';
		}
		$new_str = $str.$buqi;
		return $new_str;
	}
}
